package net.java.mediasystems.database.core.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import net.java.mediasystems.database.core.model.Genre;
import net.java.mediasystems.database.core.model.MediaType;
import net.java.mediasystems.database.core.repository.jpa.GenreRepository;
import net.java.mediasystems.database.core.service.GenreService;

/**
 * GenreService Implementation.
 */
public class GenreServiceImpl implements GenreService {
	private static final Logger LOG = LoggerFactory.getLogger(GenreServiceImpl.class);

	GenreRepository genreRepository;
	
	/**
	 * {@inheritDoc}
	 */
	@Transactional
	@Override
	public Genre addGenre(Genre genre) {
		Assert.notNull(genre);
		
		LOG.debug("Adding a new Genre.");
		return this.genreRepository.save(genre);
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Transactional
	@Override
	public List<Genre> getAllGenres() {
		return this.genreRepository.findAll();
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Transactional(readOnly = true)
	@Override
	public List<Genre> getByMediaType(MediaType mediaType) {
		return this.genreRepository.findAllByMediaType(mediaType);
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Transactional(readOnly = true)
	@Override
	public Genre getByMediaTypeAndName(MediaType mediaType, String name) {
		LOG.debug("Searching for a Genre with the Media-Type {} and the name \"{}\"", mediaType, name);
		return this.genreRepository.findByMediaTypeAndNameIgnoreCase(mediaType, name);
	}
	
	public void setGenreRepository(GenreRepository genreRepository) {
		this.genreRepository = genreRepository;
	}
}
